package cn.hjh.service;

import cn.hjh.dao.HibernateUtil;
import cn.hjh.dao.UsersDao;
import cn.hjh.pojo.Users;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;

import java.util.List;

public class UsersBiz {

    UsersDao usersDao = new UsersDao();

    /* 分页显示用户信息 */
    public List<Users> findUsersByPage(int pageNo,int pageSize){
        Transaction tx = null;
        List<Users> users = null;
        try {
            tx = HibernateUtil.currentSession().beginTransaction();
            users = new UsersDao().findUsersByPage(pageNo,pageSize);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        }
        return users;
    }

    /*  查询用户名是。。。的用户 */
    public List<Users> findUsersByName(String name){
        Transaction tx = null;
        List<Users> result = null;
        try {
            tx = HibernateUtil.currentSession().beginTransaction();
            result = usersDao.findUsersByName(name);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        }
        return result;
    }

    /* HQL查询用户表的所有用户 */
    public List<Users> findAllUsers(){
        Transaction tx = null;
        List<Users> list = null;
        try {
            tx = HibernateUtil.currentSession().beginTransaction();
            list = usersDao.findAll();
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        }
        return list;
    }

    /*按id 查询信息*/
    public Users UsersById(Integer id){
        Transaction tx = null;
        Users result = null;
        try {
            tx = HibernateUtil.currentSession().beginTransaction(); //开启事物
            result = usersDao.get(id);
            tx.commit();    //提交事物处理
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        }
        return result;
    }

    /*新增*/
    public void addNewUsers(Users users){
        Transaction tx = null;
        try {
            tx = HibernateUtil.currentSession().beginTransaction(); //开启事物
            usersDao.save(users);
            tx.commit();    //提交事物处理
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        }
    }

    /*删除*/
    public void deleteUsers(Integer id){
        Transaction tx=null;
        try {
            tx = HibernateUtil.currentSession().beginTransaction(); //开启事物
            Users usersToDelete = usersDao.get(id);
            usersDao.delete(usersToDelete);
            tx.commit();    //提交事物处理
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        }
    }

    /*修改*/
    public void updateUsers(Users users){
        Transaction tx = null;
        try {
            tx = HibernateUtil.currentSession().beginTransaction(); //开启事物
            //通过get（）或load（）方法加载要修改的对象
            Users usersToUpdate = usersDao.get(users.getId());
            //更新用户数据
            usersToUpdate.setName(users.getName());
            tx.commit();    //提交事物处理
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        }
    }

    /*使用merge（）方法修改用户信息*/
    public Users mergeUsers(Users users){
        Transaction tx = null;
        Users users1 = null;
        try {
            tx = HibernateUtil.currentSession().beginTransaction(); //开启事物
            //合并users的数据，返回持久态对象
            users1 = usersDao.merge(users);
            tx.commit();    //提交事物处理
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        }
        return users1;
    }
}
